local Element = import("./Element.lua");

local Button = commonlib.inherit(Element);

Button:Property("Pressed", false, "IsPressed");
Button:Property("DefaultStyle", "width: 80px; height: 32px; border: 1px solid #cccccc; display: flex; justify-content: center; align-items: center; line-height: 1;");
Button:Property("PressedStyle");

function Button:ctor()
    self:SetName("Button");
end

function Button:Init(xmlnode, window, parent)
    Button._super.Init(self, xmlnode, window, parent);

    self:OnAttrValueChange("pressed-style", self:GetAttrStringValue("pressed-style", "border: 1px solid #0000ff;"));

    return self;
end

function Button:OnAttrValueChange(attrName, attrValue)
    if (attrName == "pressed-style") then
        self:SetPressedStyle(self:ParseStyle(attrValue));
    end
end

function Button:ApplyStyle()
    Button._super.ApplyStyle(self);

    local pressed_style = self:GetPressedStyle();
    local current_style = self:GetCurrentStyle();
    
    self.__default_current_style__ = current_style;
    self.__pressed_current_style__ = commonlib.copy(self.__default_current_style__);
    
    for key, val in pairs(pressed_style) do
        self.__pressed_current_style__[key] = val;
    end
end

function Button:OnMouseDown(event)
    if (event:IsLeftButton()) then
        self:CaptureMouse();
        self:SetPressed(true);

        self:GetStyle():SetCurrentStyle(self.__pressed_current_style__);
        self:UpdateLayout();
    end
end

function Button:OnMouseUp(event)
    if (event:IsLeftButton()) then
        self:ReleaseMouse();
        self:SetPressed(false);

        self:GetStyle():SetCurrentStyle(self.__default_current_style__);
        self:UpdateLayout();
    end
end

return Button;